<template>
    <view>
        <jushi-signature :settings="settings" @change="signatureChange"></jushi-signature>
        <!-- <view class="" style="margin-top: 20rpx;">
            <text class="text">保存后的签名图片</text>
            <view class="preview">
                <image :src="imgUrl" mode="" style="width: 100%;"></image>
            </view>
        </view> -->
		<view>
			<u-toast ref="uToast" />
		</view>
    </view>
</template>

<script>
	import config from '../../../config.js'
	import bmap from '../../../static/js/bmap-wx.js'
	
	// #ifdef MP-WEIXIN
	let plugin = requirePlugin("WechatSI");
	let manager = plugin.getRecordRecognitionManager();
	// #endif
	
    export default {
        data() {
            return {
                settings:{ //签名设置
                    width: '750',//签名区域的宽
                    height: '500',//签名区域的高
                    lineWidth:3,//签名时线宽
                    textColor:'#007AFF' //签名文字颜色
                },
                imgUrl: '',
				rid:'',
				BMap:{},
				flex:'',
            }
        },
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			// 新建百度地图对象 
			//#ifdef MP-WEIXIN
			this.BMap = new bmap.BMapWX({ 
				ak: 'ESWpVgDaBgDk7KWsgkUkBE6kzAN0BKTa' 
			});
			//#endif
			
			//百度地图获取当前位置 示例
			// this.BMap.regeocoding({
			// 	fail:function(res){
			// 		uni.showToast({
			// 			title: '获取定位信息失败，请重试',
			// 			duration: 2000,
			// 			icon:'none'
			// 		});
			// 	},
			// 	success:function(res){
			// 		const currentAddress = res.wxMarkerData[0].address+res.wxMarkerData[0].desc
			// 		const latitude = res.wxMarkerData[0].latitude
			// 		const longitude = res.wxMarkerData[0].longitude
			// 	}
			// })
			
			//百度地图解析地址为经纬度 示例
			// this.BMap.geocoding({
			// 	address: '山东省济南市', 
			// 	fail: function (res) {
			// 		//地址转经纬度失败
			// 	},
			// 	success: function (res) {
			// 		const latitude = res.wxMarkerData[0].latitude
			// 		const longitude = res.wxMarkerData[0].longitude
			// 	}
			// }); 
			
			//注意:将代码放在onLoad方法下
			//#ifdef MP-WEIXIN
			if (!wx.getStorageSync('hadAuthed')) {
				wx.login({
				  success (res) {
				    if (res.code) {
					  uni.request({
					  	method:"POST",
					  	header:{
					  		'Authorization' :uni.getStorageSync('token'),
					  	},
					  	url: config.root +'/rest/transport/getopenId?code='+res.code,
					  	data:{
					  	},
					  	success: (res) => {
					  		if(res.data.code == 200) {
								if(res.data.data != null && res.data.data != ''){
									wx.setStorageSync('openId', res.data.data)
									wx.setStorageSync('hadAuthed', true)
								}
					  		} 
					  	}
					  })
				    } else {
				      console.log('登录失败！' + res.errMsg)
				    }
				  }
				})
			}
			//#endif
			this.rid = open.rid
		},
        methods: {
            signatureChange(e) {
                this.imgUrl = e
				if(e == "" || e == null){
					return
				}
				var that = this
				var openId = wx.getStorageSync('openId')
				uni.getLocation({
					type:'gcj02',
					altitude:false,
					success: function (res) {
						const latitude = res.latitude;
						const longitude = res.longitude;
						uni.request({
							method:"POST",
							header:{
								'Authorization' :uni.getStorageSync('token'),
							},
							url: config.root +'/rest/transportCar/editsongdaqianmingjiaoyan?id='+that.rid+'&longitude='+longitude+'&latitude='+latitude+'&openId='+openId,
							data:{
							},
							success: (res) => {
								if(res.data.code == 200) {
									uni.showLoading({
										title: '正在提交'
									})
									uni.uploadFile({
										url: config.root + 'rest/transportCar/uploadqianmingFile' +'?longitude='+longitude+'&latitude='+latitude, // 文件上传路径
										filePath: that.imgUrl,
										name: 'file',
										header: {
											'Authorization' :uni.getStorageSync('token'),
										},
										formData: {
											id:that.rid,
											openId:openId
										},
										success: res => {
											uni.hideLoading()
											let data = JSON.parse(res.data);
											if(data.code == 200) {
												// #ifdef MP-WEIXIN
													plugin.textToSpeech({
														lang: "zh_CN",
														tts: true,
														content: '恭喜您，送达成功，等待本单完成后，可以针对门店进行评价建议',
														success: function(res) {
															let music = null;
															music = uni.createInnerAudioContext(); //创建播放器对象
															music.src = res.filename;
															music.play(); //执行播放
															music.onEnded(() => {
																//播放结束
																music = null;
															});
														},
														fail: function(res) {
															 // console.log("fail tts", res)
														}
													})
												// #endif
												
												uni.showToast({
													title: '上传成功',
													duration: 2000
												});
												setTimeout(() => {
													that.$Router.back()
												}, 1000)
												
											} else {
												uni.showToast({
													title: data.message,
													duration: 2000,
													icon:'none',
												});
											}
										}
									})
								} 
								if(res.data.code == 306) {
									uni.showModal({
										content: '您所在位置超出收货范围，是否提交地址修改申请？',
										success: (res) => {
											if (res.confirm) {
												uni.request({
													method:"POST",
													header:{
														'Authorization' :uni.getStorageSync('token'),
													},
													url: config.root +'/rest/transportCar/addressEditapplyfor_songdaqianming?rid='+that.rid+'&longitude='+longitude+'&latitude='+latitude,
													data:{
													},
													success: (res) => {
														if(res.data.code == 200) {
															uni.showToast({
																title: '提交成功，请等待审核后重试',
																duration: 2000
															});
														} else {
															uni.showToast({
																title: res.data.message,
																duration: 2000,
																icon:'none',
															});
														}
													}
												})
											}
										},
									})
								}
								if(res.data.code == 305) {
									uni.showModal({
										content: res.data.message,
										showCancel: false,
										success: (res) => {
										}
									});
								}
							}
						})
					},
					fail:function(res){
						uni.showToast({
							title: '获取定位信息失败，请重试',
							duration: 2000,
							icon:'none',
						});
					},
				})
			}
        }
    }
</script>

<style>
    .preview{
        margin: 10rpx;
        border: 1rpx solid #aaaaaa;
        border-radius: 10rpx;
    }
    .text {
        margin: 20rpx;
        color: #aaaaaa;
    }
</style>